package models

import (
	"encoding/xml"
	"time"
)

//消息类型
const (
	TEXT  = "text"
	PIC   = "image"
	VIDEO = "video"
	VOICE = "voice"
	AUDIO = "audio"
	MUSIC = "music"
	//图文消息
	NEW = "news"
	//位置
	LOCATION = "location"
	EVENT    = "event"
	//事件类型
	SUBSRIBE    = "subscribe"
	UNSUBSCRIBE = "unsubscribe"
	SCAN        = "scan"
	//点击自定义菜单
	CLICK = "CLICK"
)

var ()

//易信消息结构,包括所有消息格式,文本,音乐,图片,新闻.....`xml:",omitempty"` 无内容不输出
type Message struct {
	XMLName                                                                                            xml.Name `xml:"xml"`
	ToUserName, FromUserName, MsgType, Content, Label, MediaId, Event, EventKey, MimeType, Url, PicUrl string   `xml:",omitempty"`
	Scale, ArticleCount                                                                                int      `xml:",omitempty"`
	CreateTime, MsgId                                                                                  int64    `xml:",omitempty"`
	Location_X, Location_Y                                                                             float64  `xml:",omitempty"`
	Articles                                                                                           []Article
	Music                                                                                              []Music
	Image                                                                                              []Image
}

type Music struct {
	Title, Description, MusicUrl, HQMusicUrl, ThumbMediaId string `xml:",omitempty"`
}

type Article struct {
	Item []Item `xml:"item"`
}

type Item struct {
	Title, Description, PicUrl, Url string
}

//微信返回图片信息
type Image struct {
	MediaId string
}

func init() {

}

//复制公共消息体
func (f *Message) copyCommon(to *Message) {
	to.FromUserName = f.ToUserName
	to.ToUserName = f.FromUserName
	//设置发送时间
	to.CreateTime = time.Now().Unix()
	to.MsgId = f.MsgId
}

//转换为文本消息
func (f *Message) ToTextMsg(content string) (to *Message) {
	to = &Message{}
	f.copyCommon(to)
	to.MsgType = TEXT
	to.Content = content

	return
}

//转换为图片消息
func (f *Message) ToPicMsg(picUrl string) (to *Message) {
	to = &Message{}
	f.copyCommon(to)
	to.MsgType = PIC
	to.PicUrl = picUrl
	return
}

//转换为音乐消息
func (f *Message) ToMusicMsg(title, description, musicUrl, hQMusicUrl, thumbMediaId string) (to *Message) {
	to = &Message{}
	f.copyCommon(to)
	to.MsgType = PIC
	to.Music = append(to.Music, Music{Title: title, Description: description, MusicUrl: musicUrl, HQMusicUrl: hQMusicUrl, ThumbMediaId: thumbMediaId})
	return
}

//转换为图文消息
func (f *Message) ToNewsMsg(item []Item) (to *Message) {
	to = &Message{}
	f.copyCommon(to)
	to.MsgType = NEW
	to.Articles = append(to.Articles, Article{Item: item})
	to.ArticleCount = len(to.Articles[0].Item)
	return
}

//转换为微信图片回复
func (f *Message) TOWxPicMsg(mediaId string) (to *Message) {
	to = &Message{}
	f.copyCommon(to)
	to.MsgType = PIC
	to.Image = append(to.Image, Image{mediaId})
	return
}
